<?php

class Orders extends Admin_Controller
{
    function __construct()
    {

        parent::__construct();
        error_reporting(E_ALL ^ E_NOTICE);
        $this->load->helper('url_helper');
        $this->load->helper('url');
        $this->load->model('orders_model','orders');
    }
    function order_sync(){
        $post = $this->input->post();
        log_message("error",json_encode($post));
        $data = $this->orders->order_sync($post['order_sn'],$post['app_customer_id'],$post['order_status'],$post['time'],$post['orders'],$post['returns'],$post['discount']);
        echo json_encode($data);
    }

    function xiufu(){
        $order_id = $_GET['order_id'];
        $sql = "select * from customer_order where id =".$order_id;
        $info = $this->db->query($sql)->row_array();
        $manager_id = $info['manager_id'];
        $money = $info['order_money'];
        $order_bn = $info['order_bn'];
        $time = $info['payment_date'];
        find_all_father_id_month($this->db,$manager_id,$time,$rr);
        // 总业绩
        $rr[] = $manager_id;
//                    $date = date("Y-m",time());
        $date = '2017-12';
        foreach ($rr as $v)
        {
            $bl = $this->users_model->add_finish_norm($v,$date,$money);
            if($bl)
            {
                // push_money表存入money
                $oinfo = $this->db->where(array("order_bn" => $order_bn))->get("customer_order")->row_array();
                if($oinfo)
                {
                    if($v == $manager_id)
                    {
                        // 没有上级ID
                        $v = null;
                    }
                    $this->db->insert("push_money", array('manager_id'=>$manager_id,'money'=>$money,'push_money'=>0,'status'=>'未结算','created_at'=>$time,'order_id'=>$oinfo['id'],'parent_id'=>$v));
                }

            }
        }
        // 个人业绩 $my_performance
        $this->users_model->add_finish_norm($manager_id,$date,'','','','',$money);
        //如果存在父级，给父级的直招under_performance
        $sql = "select * from manager_bind where manager_id = " .$manager_id ;
        $row = $this->db->query($sql)->row_array();
        if ($row){
            $this->users_model->add_finish_norm($row['parent_id'],$date,0,0,0,0,0,$money);
        }
        print_r($row);
    }

    function order_status($status=0){
        $arr = array(
            "0"=>"等待买家付款",
            "1"=> "买家已付款",
            "2"=>"卖家已发货",
            "3"=>"交易成功",
            "8"=>"交易结束",
            "9"=>"交易关闭",
            "10"=>"退货中",
            "11"=>"已退货"
        );
        return $arr[$status];
    }



    //获取订单列表 add by chendm 2017-9-4 
    public function order_list(){
        $manager_id = $_GET['id'];
        $limit = (isset($_GET['limit']) && !empty($_GET['limit'])) ? $_GET['limit'] : 3;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $finish_status = (isset($_GET['finish_status']) && !empty($_GET['finish_status'])) ? $_GET['finish_status'] : 'all';
        $commission_status = (isset($_GET['commission_status']) && !empty($_GET['commission_status'])) ? $_GET['commission_status'] : '0';
        $search = isset($_GET['search']) ? $_GET['search'] : '';

        if($manager_id){
            $res = $this->orders->getOrderList($manager_id, $finish_status,$commission_status,$search,$limit,$page);
            if($res['status'] == 'success'){
                $data['msg'] = "获取成功";
                $data['status'] = "success";
                $data['data'] = $res['data'];
            }else{
                $data['msg'] = $res['msg'] ;
                $data['status'] = "fail";
                $data['data'] = "";
            }
        }else{
            $data['msg'] = "参数错误";
            $data['status'] = "fail";
            $data['data'] = "";
        }
        // echo '<pre/>';print_r($data);exit; 
        echo json_encode($data);
    }

    //订单详情 add by chendm 2017-9-5 
    public function order_detail(){
        $manager_id = $_GET['id'];
        $order_id = $_GET['order_id'];

        if($order_id && $manager_id){
            $res = $this->orders->getOrderDetail($manager_id,$order_id);
            if($res['status'] == 'success'){
                $data['msg'] = "获取成功";
                $data['status'] = "success";
                $data['data'] = $res['data'];
            }else{
                $data['msg'] = $res['msg'] ;
                $data['status'] = "fail";
                $data['data'] = "";
            }
        }else{
            $data['msg'] = "参数错误";
            $data['status'] = "fail";
            $data['data'] = "";
        }
        // echo '<pre/>';print_r($data);exit;
        echo json_encode($data);
    }


    function md5_get(){
        echo md5_key();
    }



}